package com.zx.exception;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class WebFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return "pre";//请求之前过滤
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("进入前台网关过滤器.....");

        //管理员

        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();

        if(request.getMethod().equals("OPTIONS")){
            return null;
        }
        String url=request.getRequestURL().toString();
        if(url.indexOf("/user/login")>0){
            System.out.println("登陆页面"+url);
            return null;
        }



        //先验证是否携带token   {"Authorization":"bearer token"}
        //先获取是否有指定key值的头信息
        String header = request.getHeader("Authorization");

        if(header != null){
            currentContext.addZuulRequestHeader("Authorization",header);
        }
        return null;
    }

}
